//
//  ViewController.swift
//  ShareDemo
//
//  Created by Apple on 2018/8/14.
//  Copyright © 2018年 Apple. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       
        navigationBarItemShare()
    }
    
    // 分享
    func navigationBarItemShare() {
       
        guard let shareImage = UIImage.init(named: "12222") else { return }
        //创建分享消息对象
        let messageObject = UMSocialMessageObject()
        //创建网页内容对象
        //        let thumbURL = "https://mobile.umeng.com/images/pic/home/social/img-1.png"
        let shareObject = UMShareWebpageObject.shareObject(withTitle: "122",
                                                           descr: "1222",
                                                           thumImage: shareImage)
        //设置网页地址
        shareObject?.webpageUrl = "www.baidu.com"
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject
        
        let platforms: [Int] = [UMSocialPlatformType.wechatSession.rawValue,
                                UMSocialPlatformType.wechatTimeLine.rawValue,
                                UMSocialPlatformType.QQ.rawValue,
                                UMSocialPlatformType.qzone.rawValue,
                                UMSocialPlatformType.sina.rawValue]
        // notaWarning: - 要注意，传入的是一个整型的数组
        UMSocialUIManager.setPreDefinePlatforms(platforms)
        UMSocialUIManager.showShareMenuViewInWindow { (platformType: UMSocialPlatformType, userInfo: [AnyHashable : Any]?) in
            // 根据获取的platformType确定所选平台进行下一步操作
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: self, completion: { (data: Any?, error: Error?) in
                if error != nil {
                    let err = error! as NSError
                    var errorTitle = "分享失败"
                    var errorMessage: String?
                    switch err.code {
                    case UMSocialPlatformErrorType.unknow.rawValue:
                        errorMessage = "未知错误，请重试"
                    case UMSocialPlatformErrorType.notSupport.rawValue:
                        if platformType == .QQ || platformType == .qzone {
                            errorMessage = "QQ版本不支持，请更新QQ"
                        }else{
                            errorMessage = "微信版本不支持，请更新微信"
                        }
                    case UMSocialPlatformErrorType.authorizeFailed.rawValue:
                        errorMessage = "授权失败，请重试"
                    case UMSocialPlatformErrorType.shareFailed.rawValue:
                        break
                    case UMSocialPlatformErrorType.shareDataTypeIllegal.rawValue:
                        errorMessage = "分享内容不支持"
                    case UMSocialPlatformErrorType.cancel.rawValue:
                        errorTitle = "取消分享"
                    case UMSocialPlatformErrorType.notNetWork.rawValue:
                        errorMessage = "网络异常，请重试"
                    case UMSocialPlatformErrorType.notInstall.rawValue:
                        if platformType == .QQ || platformType == .qzone {
                            errorMessage = "未安装QQ，请安装后重试"
                        }else{
                            errorMessage = "未安装微信，请安装后重试"
                        }
                    case UMSocialPlatformErrorType.sourceError.rawValue:
                        errorMessage = "第三方错误，请重试"
                    default:
                        break
                    }

                } else {

                    //                    if let resp = data as? UMSocialShareResponse {
                    //                        //分享结果消息
                    //                        print("response message is \(resp.message)")
                    //                        //第三方原始返回的数据
                    //                        print("response originalResponse data is \(resp.originalResponse)")
                    //                    }else{
                    //                        print("response data is \(data)")
                    //                    }
                }
            })
        }
    }
    


}

